var app = angular.module('app', []);
app.controller('ctl.login', ['$scope', '$rootScope', '$http',
    function ($scope, $rootScope, $http) {
        $scope.login = function () {
            if ($scope.userName == '' || $scope.pwd == '' || $scope.pwd2 == '') {
                $scope.errorMessage = "请填写完整信息！";
                return;
            }

            if(  $scope.pwd !=  $scope.pwd2){
                $scope.errorMessage = "两次填写密码不一致!！";
                return;
            }

            if (!isEmpty($scope.pwd)) {
                $scope.pwd = hex_md5($scope.pwd);
            }

            $http({
                method: 'POST',
                url: 'user/register',
                params: {
                    userName: $scope.userName,
                    pwd: $scope.pwd,
                },
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                }
            }).success(function (resp, header, config, status) {
                if (resp.code == 0) {
                    resp.data.userType="user";
                    setCookie('USERNAME', $scope.userName, 7);
                    setSessionJson('curUserObj',resp.data);
                    location.href = location.origin +webPath ;
                } else {
                    $scope.errorMessage = resp.msg;
                    $scope.getValidateCode();
                }
            }).error(function (resp, header, config, status) {
                $scope.errorMessage = '程序错误，访问数据失败';
            });
        };


        $scope.goRegister = function () {
            var url = 'login.html';
            location.replace(url);
        }


    }]);
